do not emit all the events which are stored in the memory

oroce vor 8 Jahren
Ursprung
Commit
b9cae9ad57
2 geänderte Dateien mit 28 neuen Zeilen und 4 gelöschten Zeilen
  1. 13 4
      app/models/agents/delay_agent.rb
  2. 15 0
      spec/models/agents/delay_agent_spec.rb

+ 13 - 4
app/models/agents/delay_agent.rb

@@ -11,13 +11,16 @@ module Agents
11 11
 
12 12
       `expected_receive_period_in_days` is used to determine if the Agent is working. Set it to the maximum number of days
13 13
       that you anticipate passing without this Agent receiving an incoming Event.
14
+
15
+      `max_emitted_events` is used to limit the number of the maximum events which should be created. If you omit this DelayAgent will create events for every event stored in the memory.
14 16
     MD
15 17
 
16 18
     def default_options
17 19
       {
18 20
         'expected_receive_period_in_days' => "10",
19 21
         'max_events' => "100",
20
-        'keep' => 'newest'
22
+        'keep' => 'newest',
23
+        'max_emitted_events' => "100"
21 24
       }
22 25
     end
23 26
 
@@ -55,11 +58,17 @@ module Agents
55 58
 
56 59
     def check
57 60
       if memory['event_ids'] && memory['event_ids'].length > 0
58
-        received_events.where(id: memory['event_ids']).reorder('events.id asc').each do |event|
61
+        events = received_events.where(id: memory['event_ids']).reorder('events.id asc')
62
+
63
+        if options['max_emitted_events'].present?
64
+          events = events.slice(0, options['max_emitted_events'].to_i)
65
+        end
66
+
67
+        events.each do |event|
59 68
           create_event payload: event.payload
69
+          memory['event_ids'].delete(event.id)
60 70
         end
61
-        memory['event_ids'] = []
62 71
       end
63 72
     end
64 73
   end
65
-end
74
+end

+ 15 - 0
spec/models/agents/delay_agent_spec.rb

@@ -108,5 +108,20 @@ describe Agents::DelayAgent do
108 108
 
109 109
       expect(agent.memory['event_ids']).to eq []
110 110
     end
111
+
112
+    it "re-emits max_emitted_events and clears just them from the memory" do
113
+      agent.options['max_emitted_events'] = 1
114
+      agent.receive([first_event, second_event, third_event])
115
+      expect(agent.memory['event_ids']).to eq [second_event.id, third_event.id]
116
+
117
+      expect {
118
+        agent.check
119
+      }.to change { agent.events.count }.by(1)
120
+
121
+      events = agent.events.reorder('events.id desc')
122
+      expect(agent.memory['event_ids']).to eq [third_event.id]
123
+      expect(events.first.payload).to eq second_event.payload
124
+
125
+    end
111 126
   end
112 127
 end